/*************************************
* @update: 2021-06-24   LiLinBin  [创建文件]
*
* @description: 改变属性,改变数据之后,更新组件
此mixin会自动注入到每个组件当中
*
* 注: VCloud平台最终不导入此文件
****************************************/


export default {
    des: "改变属性",
    props: {},
    data: () => ({
        commonRepeatDoTimeId: null //定时器
    }),
    created () { },
    mounted () {
        this.initCommonChangeMixin()
    },
    watch: {},
    methods: {
        initCommonChangeMixin () {
            //在调试工具下 this.dataConfig 为null
            if (this.dataConfig && this.dataConfig.isAutoUpdate) this.commonRepeatDo()
        },


        //改变数据配置项
        mixinChangeDataConfig (newData) {
            clearTimeout(this.commonRepeatDoTimeId) //去掉轮询 //定时器

            //调用组件的方法
            this.changeDataConfig && this.changeDataConfig(newData)

            if (newData.isAutoUpdate === true) {
                this.commonRepeatDo(newData) //重新开启轮询
            }
        },


        //轮询接口
        commonRepeatDo (newData) {
            newData = newData || this.dataConfig || {}

            const { isAutoUpdate, time = '' } = newData
            if (isAutoUpdate == false || time.trim() == '') return

            const [ms = 0, sec = 0, min = 0, hour = 0] = time.trim().split(':').map(s => s.trim()).reverse()
            const sumTime = ms * 1 + sec * 1000 + min * 60 * 1000 + hour * 60 * 60 * 1000

            const repeatFn = () => {
                clearTimeout(this.commonRepeatDoTimeId) //定时器
                this.commonRepeatDoTimeId = setTimeout(() => { //定时器
                    if (isAutoUpdate == false) {
                        clearTimeout(this.commonRepeatDoTimeId) //定时器
                        return
                    }
                    //调用组件的方法
                    this.repeatDo && this.repeatDo()

                    repeatFn()
                }, sumTime)
            }

            repeatFn()
        },


        //获取数据
        async getData ({ newParams } = {}) {
            //this.dataConfig是changeMixin中的
            //this.interactDataConfig是'/ProjectFrame/projects/common/mixin/interactMixin.js'中的
            const {
                source,
                staticData,
                url,
                type = 'get',
                params = {},
                dataFilterJs,
                root,
                dbSql,
                databaseParams
            } = this.interactDataConfig || this.dataConfig || {}

            //静态数据, 文件
            if (['static', 'file'].indexOf(source) !== -1) {
                return _.cloneDeep(staticData) || []
            }

            //数据库查询
            if (source === 'database') {
                databaseParams.sql = window.btoa(dbSql)
                const result =
                    (await axios.post('/mock/api/source/TEST', databaseParams)) || {}
                return result.data
            }

            //数据接口
            const myUrl =
                source === 'superSource'
                    ? `${root || window.superSourceRoot}QPROXY${url}`
                    : root ? `${root}/${url}` : url

            //接收3个变量的参数值   [this.newParams在index.vue中赋值]
            newParams = newParams || this.newParams || {}
            const allParams = { ...params, ...newParams }
            const myParams = type === 'get' ? { params: allParams } : allParams

            const result = (await axios[type](myUrl, myParams)) || {}

            return T.resDataFilter(result.data || [], dataFilterJs)
        }
    },
    beforeUnmount () {
        clearTimeout(this.commonRepeatDoTimeId) //定时器
    },
    unmounted () {
        clearTimeout(this.commonRepeatDoTimeId) //定时器
    }
}